package dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import beans.Favourite;
import beans.Follow;
import beans.Restaurant;
import beans.User;

public class FollowDAO {
	private HibernateTemplate hibernateTemplate;
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	public void saveFollow(Follow follow){
		this.hibernateTemplate.save(follow);
	}
	public void updateFollow(Follow follow){
		this.hibernateTemplate.update(follow);
	}
	public void deleteFollow(Follow follow){
		this.hibernateTemplate.delete(follow);
	}
	public List<Follow> GetAllFollows(){
		return this.hibernateTemplate.loadAll(Follow.class);
	}
	public Follow findFollow(User user){
		Follow follow = new Follow();
		for(Follow x: GetAllFollows()){
			if(x.getUsername().equalsIgnoreCase(user.getUsername())){
				follow.setId(x.getId());
				follow.setUsername(x.getUsername());
				follow.setRestaurant_id(x.getRestaurant_id());
				return follow;
			}
		}
		return null;
	}
	public Follow findFollow(User user, Restaurant restaurant){
		Follow follow = new Follow();
		for(Follow x: GetAllFollows()){
			if((x.getUsername().equalsIgnoreCase(user.getUsername())&&(x.getRestaurant_id()==restaurant.getId()))){
				follow.setId(x.getId());
				follow.setUsername(x.getUsername());
				follow.setRestaurant_id(x.getRestaurant_id());
				return follow;
			}
		}
		return null;
	}
}
